
  <body class="layui-anim layui-anim-up">
    <div class="x-nav">
      <span class="layui-breadcrumb">
        <a href="">首页</a>
        <a href="">演示</a>
        <a>
          <cite>导航元素</cite></a>
      </span>
      <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:30px">ဂ</i></a>
    </div>
    <div class="x-body">
      <div class="layui-row">
        <form class="layui-form layui-col-md12 x-so">
          <input type="text" name="rule_name"  placeholder="规则名" autocomplete="off" class="layui-input" value="管理员列表" autocomplete="off">
          <input type="text" name="rule_url"  placeholder="规则url" autocomplete="off" class="layui-input" value="admin/auth/admin" autocomplete="off">
          <div class="layui-input-inline">
           <select name="cate_name" lay-verify="required">
              <option value="帖子相关">帖子相关</option>
              <option value="用户相关">用户相关</option>
              <option value="首页相关">首页相关</option>
              <option value="管理员相关">管理员相关</option>
            </select>
          </div>
          <button class="layui-btn"  lay-submit="" lay-filter="sreach"><i class="layui-icon">&#xe615;</i>增加</button>
        </form>
      </div>
      <table class="layui-table" id="userlist" lay-filter="userlist"></table>

    </div>

    <script type="text/html" id="statusChange">
      <input type="checkbox" lay-skin="switch" lay-text="正常|封禁" name="lock" value="{{d.user_id}}" title="锁定" lay-filter="status" {{ d.status == 0 ? 'checked' : '' }}>
    </script>


    <script type="text/html" id="usertool">
      <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
      <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>
    <script>
      layui.use(['table','form','laydate'], function(){
        var table = layui.table;
        var form = layui.form;


        
        table.render({
          elem: '#userlist'
          ,url:'{:url('admin/auth/rule')}'
          ,limit:10
          ,cols: [[
            {field:'rule_id', width:80, title: 'ID', sort: true}
            ,{field:'rule_name', minWidth:120, title: '规则名'}
            ,{field:'rule_url', minWidth:120, title: '规则'}
            ,{field:'cate_name', width:150, title: '规则分类', sort: true}
            ,{fixed: 'right', title:'操作', toolbar: '#usertool', width:120}
          ]]
          ,id: 'userReload'
          ,page: true
        });

        form.on('switch(status)', function(obj){

          $.post('{:url('admin/user/changestatus')}', {user_id: this.value}, function(data, textStatus, xhr) {
              if(data.code==1){
                layer.msg(data.msg);
              }else{
                $('.layui-laypage-btn').click();
                layer.msg(data.msg);
              }
          });

        });


        form.on('submit(sreach)', function(data){
          console.log(data.field)

          $.post('{:url('admin/auth/rule_add')}',data.field , function(data, textStatus, xhr) {

            if(data.code==1){
                layer.msg(data.msg);

                table.reload('userReload', {
                  page: {
                    curr: 1 //重新从第 1 页开始
                  }
                });
            }else{
              layer.msg(data.msg);
            }
          });

          
          return false;
        });


        //监听行工具事件
        table.on('tool(userlist)', function(obj){
          var data = obj.data;

          console.log(data);
          //console.log(obj)
          if(obj.event === 'del'){

            layer.confirm('真的删除行么', function(index){
                $.post('{:url('admin/user/del')}', {user_id: data.user_id}, function(data, textStatus, xhr) {
                  if(data.code==1){
                    $('.layui-laypage-btn').click();
                    layer.msg(data.msg);
                  }else{
                    layer.msg(data.msg);
                  }
                });
            });

          } else if(obj.event === 'edit'){
            layer.open({
              type: 2,
              area: ['700px', '450px'],
              fixed: false, //不固定
              maxmin: true,
              content: '{:url('admin/user/edit')}?user_id='+data.user_id
            });
          }
        });


      });
      </script>
  </body>

</html>